<?php
	class SourceModel extends CI_Model {
		// 表名
        static $schoolSourceTable = 'school_source';
        static $schoolSourceTypeTable = 'school_source_type';
        static $schoolSourceSubjectTable = 'school_source_subject';
        static $schoolSourcePublisherTable = 'school_source_publisher';
        static $schoolSourceGradeTable = 'school_source_grade';

	    public function __construct() {
	        parent::__construct();
	    }

	    // 获取资源列表
	    public function get_source_list($params){
            // 分页
            if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
            if(isset($params['schoolId'])) $this->db->where('school_id', $params['schoolId']);
            if(isset($params['isDelete'])) $this->db->where('is_delete', $params['isDelete']);
            // 过滤格式
            if(isset($params['format'])) $this->db->where('format', $params['format']);
            // 过滤状态
            if(isset($params['state'])) $this->db->where('state', $params['state']);
            // 搜索标题
            if(isset($params['title'])) $this->db->like('title', $params['title']);
            // 过滤创建人
            if(isset($params['userId'])) $this->db->where('user_id', $params['userId']);
            // 过滤类型
            if(isset($params['typeId'])) $this->db->where('source_type_id', $params['typeId']);
            // 过滤科目
            if(isset($params['subjectId'])) $this->db->where('source_subject_id', $params['subjectId']);
            // 过滤出版社
            if(isset($params['publisherId'])) $this->db->where('source_publisher_id', $params['publisherId']);
            // 过滤年级
            if(isset($params['gradeId'])) $this->db->where('source_grade_id', $params['gradeId']);
            // 获取本校所有的资源，和其他学校共享的资源
            if(isset($params['sourceForCheck'])) {
                $schoolId = $params['sourceForCheck']['schoolId'];
                $isShared = $params['sourceForCheck']['is_shared'];
                $this->db->where("(`school_id` = {$schoolId} OR `is_shared` = {$isShared})");
            }
            // 是否只获取条数
            if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$schoolSourceTable);
            // 默认按创建时间倒序
	    	return $this->db->order_by('createtime DESC')->get(self::$schoolSourceTable)->result_array();
        }
        
        // 获取资源详情
	    public function get_source_info($sourceId){
            $this->db->where("id", $sourceId);
            return $this->db->get(self::$schoolSourceTable)->row_array();
	    }

        // 获取资源类型列表
	    public function get_source_type_list($params){
            // 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
            if(isset($params['isDelete'])) $this->db->where('is_delete', $params['isDelete']);
	    	// 是否只获取条数
            if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$schoolSourceTypeTable);
            // 默认按创建时间倒序
            if(isset($params['groupBy'])){
                $this->db->order_by($params['groupBy']);
            } else {
                $this->db->order_by('createtime DESC');
            }
	    	return $this->db->get(self::$schoolSourceTypeTable)->result_array();
        }

        // 获取资源类型详情
        public function get_source_type_info($id){
            $this->db->where('id', $id);
            $type = $this->db->get(self::$schoolSourceTypeTable)->row_array();
            return $type;
        }

        // 获取资源科目列表
	    public function get_source_subject_list($params){
            // 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
            if(isset($params['isDelete'])) $this->db->where('is_delete', $params['isDelete']);
	    	// 是否只获取条数
            if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$schoolSourceSubjectTable);
            // 默认按创建时间倒序
            if(isset($params['groupBy'])){
                $this->db->order_by($params['groupBy']);
            } else {
                $this->db->order_by('createtime DESC');
            }
	    	return $this->db->get(self::$schoolSourceSubjectTable)->result_array();
        }

        // 获取资源科目详情
        public function get_source_subject_info($id){
            $this->db->where('id', $id);
            $subject = $this->db->get(self::$schoolSourceSubjectTable)->row_array();
            return $subject;
        }

        // 获取资源出版社列表
	    public function get_source_publisher_list($params){
            // 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
            if(isset($params['isDelete'])) $this->db->where('is_delete', $params['isDelete']);
	    	// 是否只获取条数
            if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$schoolSourcePublisherTable);
            // 默认按创建时间倒序
            if(isset($params['groupBy'])){
                $this->db->order_by($params['groupBy']);
            } else {
                $this->db->order_by('createtime DESC');
            }
	    	return $this->db->get(self::$schoolSourcePublisherTable)->result_array();
        }

        // 获取资源出版社详情
        public function get_source_publisher_info($id){
            $this->db->where('id', $id);
            $publisher = $this->db->get(self::$schoolSourcePublisherTable)->row_array();
            return $publisher;
        }

        // 获取资源年级列表
        public function get_source_grade_list($params){
            if(isset($params['isDelete'])) $this->db->where('is_delete', $params['isDelete']);
            // 排序
            $this->db->order_by('`grade_pid` ASC, `grade_sort` DESC');
	    	return $this->db->get(self::$schoolSourceGradeTable)->result_array();
        }

        // 获取资源年级详情
        public function get_source_grade_info($id){
            $this->db->where('id', $id);
            $grade = $this->db->get(self::$schoolSourceGradeTable)->row_array();
            // 如果有父节点，获取父节点标题
            $grade['parent_title'] = '';
            if(!empty($grade['grade_pid'])){
                $this->db->where('id', $grade['grade_pid']);
                $parent = $this->db->get(self::$schoolSourceGradeTable)->row_array();
                $grade['parent_title'] = $parent['title'];
            }
            return $grade;
        }

        // 根据父id获取所有子节点
        public function get_source_grade_children($pid){
            $this->db->where('grade_pid', $pid);
            $this->db->where('is_delete', 0);
            return $this->db->get(self::$schoolSourceGradeTable)->result_array();
        }
	}